<?php
	class Mpayment extends CI_Model{
		public function __construct(){
			parent::__construct();
			$this->load->database();
		}
		
		
		public function list_city()
		{
			$result=$this->db->query("SELECT c.*  FROM city c ORDER BY c.pos");
			return $result->result_array();
		}
		public function list_district()
		{
			$result=$this->db->query("SELECT d.*  FROM district d ORDER BY d.pos");
			return $result->result_array();
		}
		public function list_district_city($id_city)
		{
			$result=$this->db->query("SELECT d.*  FROM district d WHERE d.id_city='$id_city' ORDER BY d.pos ");
			return $result->result_array();
		}
		
		public function save_order($custommer,$phone,$email,$address,$city,$district,$memo,$PayMenthod,$account)
		{
			$deal=strtotime("now");
			$array_cart=array();
			if(isset($_SESSION['cart']))
				$array_cart=$_SESSION['cart'];
			$tong=0;
	        foreach ($array_cart as $value =>$giatri){
	        	$tong+=($giatri[1]*$giatri[6]);
	        }
			
				$result=$this->db->query("INSERT INTO `order` (`code_order`, `id_account`, `phone`, `email`, `name`, `total`, `address`, `city`, `district`, `memo`, `payment`, `date`) 
				VALUES ('DEAL$deal', '$account', '$phone', '$email', '$custommer', '$tong', '$address', '$city', '$district', '$memo', '$PayMenthod', NOW())");
			
			$_SESSION['codedeal']="DEAL$deal";
			$id_order=$this->db->insert_id() ;
			foreach ($array_cart as $value =>$giatri){
	        	$result=$this->db->query("INSERT INTO `order_detail` (`id_order`, `id_deal`, `name`, `price`, `quantity`) 
	        	VALUES ('$id_order', '$giatri[0]', '$giatri[3]', '$giatri[6]', '$giatri[1]')");
	        }
		}
		
	}
?>